package com.rumor.background.Controller;

import com.rumor.background.Mapper.UserMapper;
import com.rumor.background.Service.WeChatLoginService;
import com.rumor.background.Utils.Utils;
import com.rumor.background.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    WeChatLoginService wc;

    // 所有查询用户的操作应该添加限制
//    @GetMapping("/queryUserList")
//    List<User> queryUserList(){
//        List<User> userList = new ArrayList<>();
//        userList.addAll(userMapper.querySCUTStudentUserList());
//        userList.addAll(userMapper.querySCUTGraduateUserList());
//        userList.addAll(userMapper.querySCUTStaffUserList());
//        userList.addAll(userMapper.queryNonSCUTUserList());
//        return userList;
//    }
//
//    @GetMapping("/querySCUTStudentUserList")
//    List<User> querySCUTStudentUserList(){
//        return userMapper.querySCUTStudentUserList();
//    }
//
//    @GetMapping("/querySCUTGraduateUserList")
//    List<User> querySCUTGraduateUserList(){
//        return userMapper.querySCUTGraduateUserList();
//    }
//
//    @GetMapping("/querySCUTStaffUserList")
//    List<User> querySCUTStaffUserList(){
//        return userMapper.querySCUTStaffUserList();
//    }
//
//    @GetMapping("/queryNonSCUTUserList")
//    List<User> queryNonSCUTUserList(){
//        return userMapper.queryNonSCUTUserList();
//    }

    @RequestMapping("/login")
    public UserImp login(@RequestParam("code") String  code){
        String open_id = wc.code2openid(code);
        if(open_id == null) return null;
        UserImp user = queryUserByOpenid(open_id);
        if(user == null){
            String username = Utils.getUsername();
            while(isUsernameUsed(username)) username = Utils.getUsername();
            user = new unregisteredUser(username);
            userMapper.addUnregisteredUser(open_id, (unregisteredUser) user);
            userMapper.addUsedUsername(username);
        }
        return user;
    }

    boolean isUsernameUsed(String username){
        return username.equals(userMapper.queryUsername(username));
    }

    UserImp queryUserByOpenid(String open_id){
        User user = userMapper.querySCUTStudentUserByOpenid(open_id);
        if(user == null) user = userMapper.querySCUTGraduateUserByOpenid(open_id);
        if(user == null) user = userMapper.querySCUTStaffUserByOpenid(open_id);
        if(user == null) user = userMapper.queryNonSCUTUserByOpenid(open_id);
        if(user == null) return userMapper.queryUnregisteredUserByOpenid(open_id);
        return (UserImp) user;
    }

    @PostMapping("/addSCUTStudentUser")
    int addSCUTStudentUser(@RequestBody SCUTStudentUser user){
        String open_id = userMapper.queryOpenidByUsernameFromUU(user.getUsername());
        int res = userMapper.addSCUTStudentUser(user, open_id);
        userMapper.deleteFromUU(open_id);
        return res;
    }

    @PostMapping("/addSCUTGraduateUser")
    int addSCUTGraduateUser(@RequestBody SCUTGraduateUser user){
        String open_id = userMapper.queryOpenidByUsernameFromUU(user.getUsername());
        int res = userMapper.addSCUTGraduateUser(user, open_id);
        userMapper.deleteFromUU(open_id);
        return res;
    }

    @PostMapping("/addSCUTStaffUser")
    int addSCUTStaffUser(@RequestBody SCUTStaffUser user){
        String open_id = userMapper.queryOpenidByUsernameFromUU(user.getUsername());
        int res = userMapper.addSCUTStaffUser(user, open_id);
        userMapper.deleteFromUU(open_id);
        return res;
    }

    @PostMapping("/addNonSCUTUser")
    int addNonSCUTUser(@RequestBody NonSCUTUser user){
        String open_id = userMapper.queryOpenidByUsernameFromUU(user.getUsername());
        int res = userMapper.addNonSCUTUser(user, open_id);
        userMapper.deleteFromUU(open_id);
        return res;
    }
}
